package tech.lq0.dreamaticvoyage.gui.screen;

import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.player.Inventory;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import tech.lq0.dreamaticvoyage.Utils;
import tech.lq0.dreamaticvoyage.block.entity.FukamizuExtractionMachineBlockEntity;
import tech.lq0.dreamaticvoyage.gui.menu.FukamizuExtractionMachineMenu;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@OnlyIn(Dist.CLIENT)
public class FukamizuExtractionMachineScreen extends AbstractContainerScreen<FukamizuExtractionMachineMenu> {

    private static final ResourceLocation TEXTURE = Utils.loc("textures/gui/fukamizu_extraction_machine.png");

    public FukamizuExtractionMachineScreen(FukamizuExtractionMachineMenu pMenu, Inventory pPlayerInventory, Component pTitle) {
        super(pMenu, pPlayerInventory, pTitle);
        imageWidth = 176;
        imageHeight = 177;
    }

    @Override
    protected void renderBg(GuiGraphics pGuiGraphics, float pPartialTick, int pMouseX, int pMouseY) {
        int i = (this.width - this.imageWidth) / 2;
        int j = (this.height - this.imageHeight) / 2;
        pGuiGraphics.blit(TEXTURE, i, j, 0, 0, this.imageWidth, this.imageHeight);

        long energy = FukamizuExtractionMachineScreen.this.menu.getEnergy();
        long progress = FukamizuExtractionMachineScreen.this.menu.getProgress();
        long maxProgress = FukamizuExtractionMachineBlockEntity.DEFAULT_PROCESS_TIME;

        // Energy
        float energyRate = Mth.clamp((float) energy / (float) FukamizuExtractionMachineBlockEntity.MAX_ENERGY, 0, 1);
        pGuiGraphics.blit(TEXTURE, i + 27, j + 66 - (int) (31 * energyRate),
                201, 0, 4, (int) (31 * energyRate));

        // Progress
        float progressRate = Mth.clamp((float) progress / (float) maxProgress, 0, 1);
        pGuiGraphics.blit(TEXTURE, i + 76, j + 50, 177, 0,
                (int) (22f * progressRate), 16);
    }

    @Override
    protected void renderTooltip(GuiGraphics pGuiGraphics, int pX, int pY) {
        super.renderTooltip(pGuiGraphics, pX, pY);

        int i = (this.width - this.imageWidth) / 2;
        int j = (this.height - this.imageHeight) / 2;

        List<Component> tooltip = new ArrayList<>();
        tooltip.add(Component.translatable("des.dreamaticvoyage.umisu_current_energy",
                FukamizuExtractionMachineScreen.this.menu.getEnergy(), FukamizuExtractionMachineBlockEntity.MAX_ENERGY));

        if ((pX - i) >= 26 && (pX - i) <= 32 && (pY - j) >= 34 && (pY - j) <= 67) {
            pGuiGraphics.renderTooltip(this.font, tooltip, Optional.empty(), pX, pY);
        }
    }

    @Override
    public void render(GuiGraphics pGuiGraphics, int pMouseX, int pMouseY, float pPartialTick) {
        this.renderBackground(pGuiGraphics);
        super.render(pGuiGraphics, pMouseX, pMouseY, pPartialTick);
        this.renderTooltip(pGuiGraphics, pMouseX, pMouseY);
    }

    @Override
    protected void init() {
        super.init();
        this.titleLabelX = 8;
        this.titleLabelY = 2;
        this.inventoryLabelX = 8;
        this.inventoryLabelY = 85;
    }
}
